package com.ll.beautymall.ui.nine.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ll.beautymall.R;
import com.ll.beautymall.adapter.ContentGvAdapter;
import com.ll.beautymall.api.UrlConfig;
import com.ll.beautymall.base.BaseFragment;
import com.ll.beautymall.bean.HomeAllBuy;
import com.ll.beautymall.ui.home.allbuy.presenter.AllBuyPresenter;
import com.ll.beautymall.ui.home.allbuy.view.IAllBuyView;
import com.ll.beautymall.utils.NetUtil;

import java.util.HashMap;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class NineContentFragment extends BaseFragment implements IAllBuyView {

    @BindView(R.id.xrv_nine_content)
    XRecyclerView xrvNineContent;
    @BindView(R.id.category_detail_rg)
    RadioGroup categoryDetailRg;

    private HashMap<String, String> parameters;
    private AllBuyPresenter allBuyPresenter;
    private int next_cursor;
    private String current_cursor = "0";
    private int page_count;
    private int current_page = 1;
    private String sort;

    private ContentGvAdapter allAdapter;

    public NineContentFragment() {
    }

    public static NineContentFragment createFragment(String category_ids, String sort) {
        NineContentFragment fragment = new NineContentFragment();
        Bundle args = new Bundle();
        args.putString("category_ids", category_ids);
        args.putString("sort", sort);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView(ViewGroup container) {
        allBuyPresenter = new AllBuyPresenter(this);

        parameters = new HashMap<>();
        Bundle bundle = getArguments();
        String category_ids = bundle.getString("category_ids");
        String sort = bundle.getString("sort");
        parameters.put(UrlConfig.Key.CATEGORY_IDS, category_ids);
        parameters.put(UrlConfig.Key.SORT, sort);
        parameters.put(UrlConfig.Key.MID, UrlConfig.Value.VALIUE_MID);
        parameters.put(UrlConfig.Key.CURSOR, current_cursor);
        parameters.put(UrlConfig.Key.COUNT, "40");
        parameters.put("part", "nine");

        configXRecyclerView();

        loadData(false);

        configRadiogroup();
    }

    private void configXRecyclerView() {
        ImageView iv_empty = new ImageView(getFActivity());
        iv_empty.setBackgroundResource(R.mipmap.img_begin_05);
        xrvNineContent.setEmptyView(iv_empty);


        XRecyclerView.LayoutManager manager = new GridLayoutManager(getFActivity(), 2, GridLayoutManager.VERTICAL, false);
        xrvNineContent.setLayoutManager(manager);

        xrvNineContent.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
                xrvNineContent.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                current_page++;
                if (current_page < page_count) {
                    loadData(true);
                    xrvNineContent.loadMoreComplete();
                }
            }
        });

        allAdapter = new ContentGvAdapter(getFActivity());
        xrvNineContent.setAdapter(allAdapter);
    }

    private void loadData(boolean isMore) {
        if (NetUtil.hasInternet(getFActivity())) {
            allBuyPresenter.setAllBuyData(isMore, parameters);
        }
    }

    @Override
    public int getFLayoutId() {
        return R.layout.fragment_nine_content;
    }

    @Override
    public void setAllBuyData(HomeAllBuy allBuy) {
        next_cursor = allBuy.getNext_cursor();
        page_count = allBuy.getPage_count();
        allAdapter.clear();
        allAdapter.setData(allBuy.getProducts());
    }

    @Override
    public void addAllBuyData(HomeAllBuy allBuy) {
        page_count = allBuy.getPage_count();
        next_cursor = allBuy.getNext_cursor();
        allAdapter.addData(allBuy.getProducts());
    }

    @Override
    public void showErrorMsg(String msg) {
        Toast.makeText(getFActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    private void configRadiogroup() {
        categoryDetailRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.category_detail_default:
                        sort = "default";
                        parameters.put(UrlConfig.Key.SORT, sort);
                        loadData(false);
                        break;
                    case R.id.category_detail_sales:
                        sort = "sales";
                        parameters.put(UrlConfig.Key.SORT, sort);
                        loadData(false);
                        break;
                    case R.id.category_detail_time:
                        sort = "time";
                        parameters.put(UrlConfig.Key.SORT, sort);
                        loadData(false);
                        break;
                }
            }
        });
    }
}
